package org.jeecg.modules.inventory.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.constant.WarehouseConstant;
import org.jeecg.modules.basicSkuInfo.entity.BasicSkuInfo;
import org.jeecg.modules.basicSkuInfo.service.IBasicSkuInfoService;
import org.jeecg.modules.inventory.entity.InventoryInfo;
import org.jeecg.modules.inventory.service.IInventoryInfoService;
import org.jeecg.modules.reportLabel.entity.ReportLabel;
import org.jeecg.modules.reportLabel.service.IReportLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 库存信息
 * @Author: jeecg-boot
 * @Date: 2024-10-17
 * @Version: V1.0
 */
@Api(tags = "库存信息")
@RestController
@RequestMapping("/inventoryInfo/inventoryInfo")
@Slf4j
public class InventoryInfoController extends JeecgController<InventoryInfo, IInventoryInfoService> {
    @Autowired
    private IInventoryInfoService inventoryInfoService;
    @Autowired
    private IReportLabelService reportLabelService;
    @Autowired
    private IBasicSkuInfoService basicSkuInfoService;

    /**
     * 分页列表查询
     *
     * @param inventoryInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "库存信息-分页列表查询")
    @ApiOperation(value = "库存信息-分页列表查询", notes = "库存信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(InventoryInfo inventoryInfo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<InventoryInfo> queryWrapper = QueryGenerator.initQueryWrapper(inventoryInfo, req.getParameterMap());
        queryWrapper.orderByAsc("FIELD(warehouse_code, 'A', 'H', 'F')");
        Page<InventoryInfo> page = new Page<InventoryInfo>(pageNo, pageSize);
        IPage<InventoryInfo> pageList = inventoryInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param inventoryInfo
     * @return
     */
    @AutoLog(value = "库存信息-添加")
    @ApiOperation(value = "库存信息-添加", notes = "库存信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody InventoryInfo inventoryInfo) {
        inventoryInfoService.save(inventoryInfo);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param inventoryInfo
     * @return
     */
    @AutoLog(value = "库存信息-编辑")
    @ApiOperation(value = "库存信息-编辑", notes = "库存信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody InventoryInfo inventoryInfo) {
        inventoryInfoService.updateById(inventoryInfo);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "库存信息-通过id删除")
    @ApiOperation(value = "库存信息-通过id删除", notes = "库存信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        inventoryInfoService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "库存信息-批量删除")
    @ApiOperation(value = "库存信息-批量删除", notes = "库存信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.inventoryInfoService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "库存信息-通过id查询")
    @ApiOperation(value = "库存信息-通过id查询", notes = "库存信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        InventoryInfo inventoryInfo = inventoryInfoService.getById(id);
        if (inventoryInfo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(inventoryInfo);
    }

    @AutoLog(value = "库存管理-添加库存至临时库区")
    @ApiOperation(value = "库存管理-添加库存至临时库区", notes = "库存管理-添加库存至临时库区")
    @PostMapping(value = "/saveInventoryToTemporary")
    public Result<?> saveInventoryToTemporary(String skuCode, Integer goodsQuantity) {
        ReportLabel reportLabel = reportLabelService.getOne(new LambdaQueryWrapper<ReportLabel>()
                .eq(ReportLabel::getWarehouseCode, WarehouseConstant.WarehouseTypeEnum.CONVERT.getCode())
                .eq(ReportLabel::getSkuCode, skuCode)
                .eq(ReportLabel::getInventoryBoxQuantity, 0)
                .eq(ReportLabel::getInventoryGoodsQuantity, 0)
                .eq(ReportLabel::getAvailableBoxQuantity, 0)
                .isNull(ReportLabel::getOutOrderId));
        if (null == reportLabel) {
            BasicSkuInfo skuInfo = basicSkuInfoService.list(new LambdaQueryWrapper<BasicSkuInfo>().eq(BasicSkuInfo::getSkuCode, skuCode)).get(0);

            reportLabel = new ReportLabel();
            reportLabel.setWarehouseCode(WarehouseConstant.WarehouseTypeEnum.CONVERT.getCode());
            reportLabel.setLabelNumber(skuCode);
            reportLabel.setSkuCode(skuCode);
            reportLabel.setInventoryBoxQuantity(0);
            reportLabel.setInventoryGoodsQuantity(0);
            reportLabel.setAvailableBoxQuantity(0);
            reportLabel.setAvailableGoodsQuantity(goodsQuantity);
            reportLabel.setShelfCode("-");
            reportLabel.setCompartmentCode("-");
            reportLabel.setSkuType(skuInfo.getSkuType());
            reportLabel.setInOrderId(null);
            reportLabel.setInOrderCode(null);
            reportLabel.setInDate(new Date());
            reportLabel.setContainerNo("-");
            reportLabel.setOutOrderId(null);
            reportLabelService.save(reportLabel);
        } else {
            reportLabel.setAvailableGoodsQuantity(reportLabel.getAvailableGoodsQuantity() + goodsQuantity);
            reportLabelService.updateById(reportLabel);
        }

        InventoryInfo inventoryInfo = inventoryInfoService.getOne(new LambdaQueryWrapper<InventoryInfo>()
                .eq(InventoryInfo::getWarehouseCode, WarehouseConstant.WarehouseTypeEnum.CONVERT.getCode())
                .eq(InventoryInfo::getSkuCode, skuCode)
                .eq(InventoryInfo::getInventoryBoxQuantity, 0)
                .eq(InventoryInfo::getInventoryGoodsQuantity, 0)
                .eq(InventoryInfo::getAvailableBoxQuantity, 0)
                .isNull(InventoryInfo::getOutOrderId));
        if (null == inventoryInfo) {
            inventoryInfo = new InventoryInfo();
            inventoryInfo.setWarehouseCode(WarehouseConstant.WarehouseTypeEnum.CONVERT.getCode());
            inventoryInfo.setShelfCode("-");
            inventoryInfo.setCompartmentCode("-");
            inventoryInfo.setSkuCode(skuCode);
            inventoryInfo.setInventoryBoxQuantity(0);
            inventoryInfo.setInventoryGoodsQuantity(0);
            inventoryInfo.setAvailableBoxQuantity(0);
            inventoryInfo.setAvailableGoodsQuantity(goodsQuantity);
            inventoryInfo.setEnterDate(new Date());
            inventoryInfo.setOutOrderId(null);
            inventoryInfoService.save(inventoryInfo);
        } else {
            inventoryInfo.setAvailableGoodsQuantity(inventoryInfo.getAvailableGoodsQuantity() + goodsQuantity);
            inventoryInfoService.updateById(inventoryInfo);
        }

        return Result.OK();
    }

    /**
     * 导出excel
     *
     * @param request
     * @param inventoryInfo
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, InventoryInfo inventoryInfo) {
        return super.exportXls(request, inventoryInfo, InventoryInfo.class, "库存信息");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, InventoryInfo.class);
    }

    @AutoLog(value = "呼叫货架-查询货架内所有货物")
    @ApiOperation(value = "呼叫货架-查询货架内所有货物", notes = "呼叫货架-查询货架内所有货物")
    @PostMapping(value = "/getSkuInfoByShelfCode")
    public Result<?> getSkuInfoByShelfCode(String shelfCode) {
        Assert.isTrue(StringUtils.isNotBlank(shelfCode), "请选择货架！");

        List<InventoryInfo> list = inventoryInfoService.list(new LambdaQueryWrapper<InventoryInfo>()
                .eq(InventoryInfo::getWarehouseCode, WarehouseConstant.WarehouseTypeEnum.AGV.getCode())
                .eq(InventoryInfo::getShelfCode, shelfCode)
                .gt(InventoryInfo::getInventoryBoxQuantity, 0));
        return Result.OK(list);
    }

    @AutoLog(value = "呼叫货架-查询货格内所有货物")
    @ApiOperation(value = "呼叫货架-查询货格内所有货物", notes = "呼叫货架-查询货格内所有货物")
    @PostMapping(value = "/getSkuInfoByCompartmentCode")
    public Result<?> getSkuInfoByCompartmentCode(String compartmentCode) {
        Assert.isTrue(StringUtils.isNotBlank(compartmentCode), "请选择货格！");

        List<InventoryInfo> list = inventoryInfoService.list(new LambdaQueryWrapper<InventoryInfo>()
                .eq(InventoryInfo::getWarehouseCode, WarehouseConstant.WarehouseTypeEnum.AGV.getCode())
                .eq(InventoryInfo::getCompartmentCode, compartmentCode)
                .gt(InventoryInfo::getInventoryBoxQuantity, 0));
        return Result.OK(list);
    }

}
